<!--
 * @Description: 商家远程任务设置弹窗
 * @Author: liyujie
 * @Date: 2021/10/12
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/10/12
-->
<template>
    <section class="wrapper">
        <el-dialog
            title="商家任务平台抽佣设置"
            :visible.sync="dialogVisible"
            width="680px"
            :close-on-click-modal="false"
            @close="$emit('close')"
        >
            <div class="dialog-content">
                <el-form
                    :model="ruleForm"
                    :rules="rules"
                    label-width="70px"
                    @submit.native.prevent
                >
                    <!--<el-form-item
                        label="平台抽佣"
                        prop="shop_commission_rate"
                    >
                        <el-input
                            v-model="ruleForm.shop_commission_rate"
                            size="medium"
                            style="width: 380px"
                            clearable
                        >
                            <span
                                slot="suffix"
                                style="margin-right: 6px"
                            >%</span>
                        </el-input>
                    </el-form-item>-->
                    <el-form-item label="文案注释">
                        <el-input
                            v-model="ruleForm.shop_commission_desc"
                            type="textarea"
                            placeholder="请输入平台抽佣描述，仅在商家端展示"
                            :autosize="{ minRows: 4}"
                        />
                    </el-form-item>
                </el-form>
            </div>
            <div
                class="dialog-footer"
                style="padding-top: 12px"
            >
                <el-button
                    size="medium"
                    style="width: 120px"
                    @click="dialogVisible = false"
                >
                    取消
                </el-button>
                <el-button
                    type="primary"
                    size="medium"
                    style="width: 120px;margin-left: 32px"
                    :disabled="!ruleForm.shop_commission_desc"
                    @click="handleConfirm()"
                >
                    确定
                </el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
import {deepClone} from "@/assets/js/utils";
import Vue from "vue";
const numberReg = /(?:^[1-9]([0-9]+)?(?:\.[0-9]{1,2})?$)|(?:^(?:0)$)|(?:^[0-9]\.[0-9](?:[0-9])?$)/;

const Dialog = {
    name: "ShopProjectConfig",
    props: {},
    data() {
        const checkPercent = (rule, value, callback) => {
            if(Number(value) > 0 && Number(value) <= 100) {
                callback();
            } else {
                callback(new Error('百分比输入不正确'));
            }
        };

        return {
            dialogVisible: false,
            ruleForm: {
                // shop_commission_rate: '',
                shop_commission_desc: ''
            },
            rules: {
                // shop_commission_rate: [
                //     {pattern: numberReg, message: '平台抽佣必须为数字', trigger: 'blur'},
                //     { validator: checkPercent, trigger: 'blur' }
                // ]
            }
        };
    },
    mounted() {
        this.getConfig();
    },
    methods: {

        /**
         * 显示弹窗
         */
        show() {
            this.dialogVisible = true;
        },

        /**
         * 显示弹窗
         */
        close() {
            this.dialogVisible = false;
        },

        /**
         * 获取配置
         */
        getConfig() {
            this.$post('/student/task_stuagent_for_shop%5Cget_commission_config', {}, resp => {
                if (resp.code === 1) {
                    this.ruleForm = resp.data;
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 点击确定
         */
        handleConfirm() {
            let params = deepClone(this.ruleForm);

            this.$post('/student/task_stuagent_for_shop%5Cset_commission_config', params, resp => {
                if (resp.code === 1) {
                    this.$notify({
                        title: '提示',
                        message: '设置成功',
                        duration: 2000,
                        type: 'success'
                    });
                    this.$emit('confirm', true);
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },
    }
};

export default Dialog;

const shopProjectConfigDialog = function (config) {
    return new Promise((resolve) => {
        const DialogConstructor = Vue.extend(Dialog);
        const instance = new DialogConstructor({
            propsData: config,
        }).$mount();

        instance.$on("close", function () {
            setTimeout(() => {
                document.body.removeChild(instance.$el);
                instance.$destroy();
            }, 300);
            resolve(false);
        });
        instance.$on("confirm", function (data) {
            instance.close();
            resolve(data);
        });
        document.body.appendChild(instance.$el);
        instance.dialogVisible = true;
    });
};

export { shopProjectConfigDialog };
</script>

<style scoped lang="less">
.wrapper {
    .dialog-content {
        display: flex;
        flex-direction: column;
        padding: 0 32px;
    }
    .dialog-footer {
        display: flex;
        justify-content: center;
    }
}

/deep/ .el-dialog__header {
    padding-top: 10px;
    box-shadow: 0 1px 0 0 rgba(233, 233, 233, 1);
    display: flex;
    align-items: center;
    background: #FAFAFA;
    border-radius: 4px 4px 0 0;

    .el-dialog__title {
        font-weight: 500;
        color: #333;
        font-size: 14px;
    }

    .el-icon-close:before {
        color: rgb(194, 194, 194);
        font-size: 17px;
        font-weight: bold;
    }

    .el-dialog__headerbtn {
        top: 14px;
    }
}

/deep/ .el-dialog__body {
    padding: 24px 32px;
}

/deep/ .el-textarea__inner {
    resize: none;
}
</style>
